package com.water.note.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.water.note.common.Result;
import com.water.note.entity.Wish;

import com.water.note.service.WishService;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 愿望清单控制器
 */
@RestController
@RequestMapping("/wish")
@RequiredArgsConstructor
public class WishController {

    private final WishService wishService;


    /**
     * 分页查询愿望清单
     *
     * @param page     页码
     * @param pageSize 每页大小
     * @param status   状态
     * @param keyword  关键字
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<Wish>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword) {

        Long userId = StpUtil.getLoginIdAsLong();
        Page<Wish> pageParam = new Page<>(page, pageSize);
        Page<Wish> pageResult = wishService.page(pageParam, userId, status, keyword);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询愿望详情
     *
     * @param id 愿望ID
     * @return 愿望详情
     */
    @GetMapping("/{id}")
    public Result<Wish> getById(@PathVariable Long id) {
        Wish wish = wishService.getById(id);
        return Result.success(wish);
    }

    /**
     * 新增愿望
     *
     * @param wish 愿望信息
     * @return 操作结果
     */
    @PostMapping
    public Result<Wish> save(@RequestBody Wish wish) {

        wish.setUserId(StpUtil.getLoginIdAsLong());
        wishService.save(wish);
        return Result.success(wish);
    }

    /**
     * 更新愿望
     *
     * @param wish 愿望信息
     * @return 操作结果
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody Wish wish) {
        boolean result = wishService.updateById(wish);
        return Result.success(result);
    }

    /**
     * 删除愿望
     *
     * @param id 愿望ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = wishService.removeById(id);
        return Result.success(result);
    }

    /**
     * 更新愿望进度
     *
     * @param id       愿望ID
     * @param progress 进度
     * @return 操作结果
     */
    @PutMapping("/{id}/progress")
    public Result<Boolean> updateProgress(@PathVariable Long id, @RequestParam Integer progress) {
        boolean result = wishService.updateProgress(id, progress);
        return Result.success(result);
    }

    /**
     * 更新愿望状态
     *
     * @param id     愿望ID
     * @param status 状态
     * @return 操作结果
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean result = wishService.updateStatus(id, status);
        return Result.success(result);
    }





} 